import {
  ref
} from 'vue'
//日期转星期
function weekdate(val) {
  let date = new Date(val);
  let weekday = date.getDay();
  let weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  let result = weekdays[weekday];
  return result
}
//根据天气转图片
function getWeatherImage(weatherDescription) {
  // 判断区分使用的是那个天气api
  const isUnicode = !isNaN(parseFloat(weatherDescription)) && isFinite(weatherDescription);
  // 定义一个映射，连接天气状况和图片文件名
  const weatherImages = ref({})
  if (isUnicode) {
    weatherImages.value = {
      100: "/static/icon/sunny.png", //昼晴
      101: "/static/icon/daylight-cloudy.png", //昼多云
      102: "/static/icon/daylight-cloudy.png", //昼少云
      103: "/static/icon/daylight-cloudy.png", //昼晴间多云
      104: "/static/icon/cloudy.png", //阴
      150: "/static/icon/night-clear.png", //夜晴
      151: "/static/icon/night-cloudy.png", //夜多云
      152: "/static/icon/night-cloudy.png", //夜少云
      153: "/static/icon/night-cloudy.png", //夜晴间多云
      300: "/static/icon/thunderstorm.png", //阵雨
      301: "/static/icon/thunderstorm.png", //强阵雨
      302: "/static/icon/thunderstorm.png", //雷阵雨
      303: "/static/icon/thunderstorm.png", //强雷阵雨
      304: "/static/icon/thunderstorm.png", //雷阵雨伴有冰雹
      305: "/static/icon/light-rain.png", //小雨
      306: "/static/icon/moderate-rain.png", //中雨
      307: "/static/icon/heavy-rain.png", //大雨
      308: "/static/icon/light-rain.png", //极端降雨
      309: "/static/icon/light-rain.png", //毛毛雨/细雨
      310: "/static/icon/rainstorm.png", //暴雨
      311: "/static/icon/rainstorm.png", //大暴雨
      312: "/static/icon/rainstorm.png", //特大暴雨
      313: "/static/icon/sleet.png", //冻雨
      314: "/static/icon/light-rain.png", //小到中雨
      315: "/static/icon/moderate-rain.png", //中到大雨
      316: "/static/icon/heavy-rain.png", //大到暴雨
      317: "/static/icon/rainstorm.png", //暴雨到大暴雨
      318: "/static/icon/rainstorm.png", //大暴雨到特大暴雨
      350: "/static/icon/thunderstorm.png", //阵雨
      351: "/static/icon/thunderstorm.png", //强阵雨
      399: "/static/icon/heavy-rain.png", //雨
      400: "/static/icon/snow-lightly.png", //小雪
      401: "/static/icon/snow-moderate.png", //中雪
      402: "/static/icon/snow-heavy.png", //大雪
      403: "/static/icon/blizzard.png", //暴雪
      404: "/static/icon/sleet.png", //雨夹雪
      405: "/static/icon/sleet.png", //雨雪天气
      406: "/static/icon/sleet.png", //阵雨夹雪
      407: "/static/icon/sleet.png", //阵雪
      408: "/static/icon/snow-lightly.png", //小到中雪
      409: "/static/icon/snow-moderate.png", //中到大雪
      410: "/static/icon/snow-heavy.png", //大到暴雪
      456: "/static/icon/sleet.png", //阵雨夹雪
      457: "/static/icon/sleet.png", //阵雪
      499: "/static/icon/snow-heavy.png", //雪
      500: "/static/icon/fog.png", //薄雾
      501: "/static/icon/fog.png", //雾
      502: "/static/icon/smog.png", //霾
      503: "/static/icon/blowing-sand.png", //扬沙
      504: "/static/icon/blowing-sand.png", //浮尘
      507: "/static/icon/blowing-sand.png", //沙尘暴
      508: "/static/icon/blowing-sand.png", //强沙尘暴
      509: "/static/icon/fog.png", //浓雾
      510: "/static/icon/fog.png", //强浓雾
      511: "/static/icon/smog.png", //中度霾
      512: "/static/icon/smog.png", //重度霾
      513: "/static/icon/smog.png", //严重霾
      514: "/static/icon/fog.png", //大雾
      515: "/static/icon/fog.png", //特强浓雾
      900: "/static/icon/sunny.png", //热
      901: "/static/icon/night-clear.png", //冷
    };
  } else {
    weatherImages.value = {
      "CLEAR_DAY": "/static/icon/sunny.png", //昼晴
      "CLEAR_NIGHT": "/static/icon/night-clear.png", //夜晴
      "PARTLY_CLOUDY_DAY": "/static/icon/daylight-cloudy.png", //昼多云
      "PARTLY_CLOUDY_NIGHT": "/static/icon/night-cloudy.png", //夜多云
      "CLOUDY": "/static/icon/cloudy.png", //阴
      "LIGHT_HAZE": "/static/icon/smog.png", //雾霾
      "MODERATE_HAZE": "/static/icon/smog.png", //雾霾
      "HEAVY_HAZE": "/static/icon/smog.png", //雾霾
      "LIGHT_RAIN": "/static/icon/light-rain.png", //小雨
      "MODERATE_RAIN": "/static/icon/moderate-rain.png", //中雨
      "HEAVY_RAIN": "/static/icon/heavy-rain.png", //大雨
      "STORM_RAIN": "/static/icon/rainstorm.png", //暴雨
      "FOG": "/static/icon/fog.png", //雾
      "LIGHT_SNOW": "/static/icon/snow-lightly.png", //小雪
      "MODERATE_SNOW": "/static/icon/snow-moderate.png", //中雪
      "HEAVY_SNOW": "/static/icon/snow-heavy.png", //大雪
      "STORM_SNOW": "/static/icon/blizzard.png", //暴雪
      "DUST": "/static/icon/blowing-sand.png", //浮尘
      "SAND": "/static/icon/blowing-sand.png", //沙尘
      "WIND": "/static/icon/strong-wind.png", //大风
    };
  }
  // 返回对应的图片路径，如果没有找到匹配的天气状况，则返回默认图片
  return weatherImages.value[weatherDescription] || "images/default.png";
}
//根据天气返回背景色
function getWeatherHomeImage(weatherDescription) {
  const weatherImages = ref({})
  weatherImages.value = {
    100: "/static/image/fine.png", //昼晴
    101: "/static/image/fine.png", //昼多云
    102: "/static/image/fine.png", //昼少云
    103: "/static/image/fine.png", //昼晴间多云
    104: "", //阴
    150: "/static/image/fine.png", //夜晴
    151: "/static/image/cloudy.png", //夜多云
    152: "/static/image/cloudy.png", //夜少云
    153: "/static/image/cloudy.png", //夜晴间多云
    300: "/static/image/rainy.png", //阵雨
    301: "/static/image/rainy.png", //强阵雨
    302: "/static/image/rainy.png", //雷阵雨
    303: "/static/image/rainy.png", //强雷阵雨
    304: "/static/image/rainy.png", //雷阵雨伴有冰雹
    305: "/static/image/rainy.png", //小雨
    306: "/static/image/rainy.png", //中雨
    307: "/static/image/rainy.png", //大雨
    308: "/static/image/rainy.png", //极端降雨
    309: "/static/image/rainy.png", //毛毛雨/细雨
    310: "/static/image/rainy.png", //暴雨
    311: "/static/image/rainy.png", //大暴雨
    312: "/static/image/rainy.png", //特大暴雨
    313: "/static/image/rainy.png", //冻雨
    314: "/static/image/rainy.png", //小到中雨
    315: "/static/image/rainy.png", //中到大雨
    316: "/static/image/rainy.png", //大到暴雨
    317: "/static/image/rainy.png", //暴雨到大暴雨
    318: "/static/image/rainy.png", //大暴雨到特大暴雨
    350: "/static/image/rainy.png", //阵雨
    351: "/static/image/rainy.png", //强阵雨
    399: "/static/image/snow.png", //雨
    400: "/static/image/snow.png", //小雪
    401: "/static/image/snow.png", //中雪
    402: "/static/image/snow.png", //大雪
    403: "/static/image/snow.png", //暴雪
    404: "/static/image/snow.png", //雨夹雪
    405: "/static/image/snow.png", //雨雪天气
    406: "/static/image/snow.png", //阵雨夹雪
    407: "/static/image/snow.png", //阵雪
    408: "/static/image/snow.png", //小到中雪
    409: "/static/image/snow.png", //中到大雪
    410: "/static/image/snow.png", //大到暴雪
    456: "/static/image/snow.png", //阵雨夹雪
    457: "/static/image/snow.png", //阵雪
    499: "/static/image/snow.png", //雪
    500: "/static/image/Foggy.png", //薄雾
    501: "/static/image/Foggy.png", //雾
    502: "/static/image/smog.png", //霾
    503: "/static/image/sandDust.png", //扬沙
    504: "/static/image/sandDust.png", //浮尘
    507: "/static/image/sandDust.png", //沙尘暴
    508: "/static/image/sandDust.png", //强沙尘暴
    509: "/static/image/Foggy.png", //浓雾
    510: "/static/image/Foggy.png", //强浓雾
    511: "/static/image/smog.png", //中度霾
    512: "/static/image/smog.png", //重度霾
    513: "/static/image/smog.png", //严重霾
    514: "/static/image/Foggy.png", //大雾
    515: "/static/image/Foggy.png", //特强浓雾
  };
  return weatherImages.value[weatherDescription] || "sunny";
}

//根据天气返回对应的图片
function getWeatherBackground(code) {
  const weatherImages = ref({})
  weatherImages.value = {
    100: "sunny", //昼晴
    101: "sunny", //昼多云
    102: "sunny", //昼少云
    103: "sunny", //昼晴间多云
    104: "clear", //阴
    150: "sunny", //夜晴
    151: "cloudy", //夜多云
    152: "cloudy", //夜少云
    153: "cloudy", //夜晴间多云
    300: "rainstorm", //阵雨
    301: "rainstorm", //强阵雨
    302: "rainstorm", //雷阵雨
    303: "rainstorm", //强雷阵雨
    304: "rainstorm", //雷阵雨伴有冰雹
    305: "rainstorm", //小雨
    306: "rainstorm", //中雨
    307: "rainstorm", //大雨
    308: "rainstorm", //极端降雨
    309: "rainstorm", //毛毛雨/细雨
    310: "rainstorm", //暴雨
    311: "rainstorm", //大暴雨
    312: "rainstorm", //特大暴雨
    313: "rainstorm", //冻雨
    314: "rainstorm", //小到中雨
    315: "rainstorm", //中到大雨
    316: "rainstorm", //大到暴雨
    317: "rainstorm", //暴雨到大暴雨
    318: "rainstorm", //大暴雨到特大暴雨
    350: "rainstorm", //阵雨
    351: "rainstorm", //强阵雨
    399: "rainstorm", //雨
    400: "sleet", //小雪
    401: "sleet", //中雪
    402: "sleet", //大雪
    403: "sleet", //暴雪
    404: "sleet", //雨夹雪
    405: "sleet", //雨雪天气
    406: "sleet", //阵雨夹雪
    407: "sleet", //阵雪
    408: "sleet", //小到中雪
    409: "sleet", //中到大雪
    410: "sleet", //大到暴雪
    456: "sleet", //阵雨夹雪
    457: "sleet", //阵雪
    499: "sleet", //雪
    500: "fog", //薄雾
    501: "fog", //雾
    502: "smog", //霾
    503: "sand-dust", //扬沙
    504: "sand-dust", //浮尘
    507: "sand-dust", //沙尘暴
    508: "sand-dust", //强沙尘暴
    509: "fog", //浓雾
    510: "fog", //强浓雾
    511: "smog", //中度霾
    512: "smog", //重度霾
    513: "smog", //严重霾
    514: "fog", //大雾
    515: "fog", //特强浓雾
  };
  return weatherImages.value[code] || "sunny";
}

//根据天气返回对应的header背景色
function getWeatherHeaderBackground(code) {
  const weatherImages = ref({})
  weatherImages.value = {
    100: "#8DADFC", //昼晴
    101: "#8DADFC", //昼多云
    102: "#8DADFC", //昼少云
    103: "#8DADFC", //昼晴间多云
    104: "#919CB8 ", //阴
    150: "#8DADFC", //夜晴
    151: "#919CB8", //夜多云
    152: "#919CB8", //夜少云
    153: "#919CB8", //夜晴间多云
    300: "#B5B5B5", //阵雨 
    301: "#B5B5B5", //强阵雨
    302: "#B5B5B5", //雷阵雨
    303: "#B5B5B5", //强雷阵雨
    304: "#B5B5B5", //雷阵雨伴有冰雹
    305: "#B5B5B5", //小雨
    306: "#B5B5B5", //中雨
    307: "#B5B5B5", //大雨
    308: "#B5B5B5", //极端降雨
    309: "#B5B5B5", //毛毛雨/细雨
    310: "#B5B5B5", //暴雨
    311: "#B5B5B5", //大暴雨
    312: "#B5B5B5", //特大暴雨
    313: "#B5B5B5", //冻雨
    314: "#B5B5B5", //小到中雨
    315: "#B5B5B5", //中到大雨
    316: "#B5B5B5", //大到暴雨
    317: "#B5B5B5", //暴雨到大暴雨
    318: "#B5B5B5", //大暴雨到特大暴雨
    350: "#B5B5B5", //阵雨
    351: "#B5B5B5", //强阵雨
    399: "#B5B5B5", //雨
    400: "#B5B5B5", //小雪
    401: "#B5B5B5", //中雪
    402: "#B5B5B5", //大雪
    403: "#B5B5B5", //暴雪
    404: "#B5B5B5", //雨夹雪
    405: "#B5B5B5", //雨雪天气
    406: "#B5B5B5", //阵雨夹雪
    407: "#B5B5B5", //阵雪
    408: "#B5B5B5", //小到中雪
    409: "#B5B5B5", //中到大雪
    410: "#B5B5B5", //大到暴雪
    456: "#B5B5B5", //阵雨夹雪
    457: "#B5B5B5", //阵雪
    499: "#B5B5B5", //雪
    500: "#8DADFC", //薄雾
    501: "#8DADFC", //雾
    502: "#8F8F8F", //霾
    503: "#DEB7A0", //扬沙
    504: "#DEB7A0", //浮尘
    507: "#DEB7A0", //沙尘暴
    508: "#DEB7A0", //强沙尘暴
    509: "#8DADFC", //浓雾
    510: "#8DADFC", //强浓雾
    511: "#8F8F8F", //中度霾
    512: "#8F8F8F", //重度霾
    513: "#8F8F8F", //严重霾
    514: "#8DADFC", //大雾
    515: "#8DADFC", //特强浓雾
  };
  return weatherImages.value[code] || "";
}
//彩云天气，天气编码转汉字
function getWeatherValue(weatherDescription) {
  // 定义一个映射，连接天气状况和图片文件名
  const weatherImages = ref({})
  weatherImages.value = {
    "CLEAR_DAY": "晴", //昼晴
    "CLEAR_NIGHT": "晴", //夜晴
    "PARTLY_CLOUDY_DAY": "多云", //昼多云
    "PARTLY_CLOUDY_NIGHT": "多云", //夜多云
    "CLOUDY": "阴", //阴
    "LIGHT_HAZE": "雾霾", //雾霾
    "MODERATE_HAZE": "雾霾", //雾霾
    "HEAVY_HAZE": "雾霾", //雾霾
    "LIGHT_RAIN": "小雨", //小雨
    "MODERATE_RAIN": "中雨", //中雨
    "HEAVY_RAIN": "大雨", //大雨
    "STORM_RAIN": "暴雨", //暴雨
    "FOG": "雾", //雾
    "LIGHT_SNOW": "小雪", //小雪
    "MODERATE_SNOW": "中学", //中雪
    "HEAVY_SNOW": "大雪", //大雪
    "STORM_SNOW": "暴雪", //暴雪
    "DUST": "浮尘", //浮尘
    "SAND": "沙尘", //沙尘
    "WIND": "大风", //大风
  };
  // 返回对应的图片路径，如果没有找到匹配的天气状况，则返回默认图片
  return weatherImages.value[weatherDescription]
}
//根据国标AQI标签判断空气质量
function getAirQuality(aqi) {
  if (aqi >= 0 && aqi <= 50) {
    return "空气优";
  } else if (aqi >= 51 && aqi <= 100) {
    return "空气良";
  } else if (aqi >= 101 && aqi <= 150) {
    return "轻度污染";
  } else if (aqi >= 151 && aqi <= 200) {
    return "中度污染";
  } else if (aqi >= 201 && aqi <= 300) {
    return "重度污染";
  } else {
    return "严重污染";
  }
}
// 判断空气质量,优良差
function getAirQualitySimple(aqi) {
  if (aqi >= 0 && aqi <= 50) {
    return "优";
  } else if (aqi >= 51 && aqi <= 100) {
    return "良";
  } else {
    return "差";
  }
}
//判断昨天，今天，明天
function checkDateRelativeToToday(dateString) {
  // 获取当前日期
  const today = new Date();
  today.setHours(0, 0, 0, 0); // 设置为当天的0点（只关心日期部分）

  // 将传入的字符串日期转化为 Date 对象
  const inputDate = new Date(dateString);
  inputDate.setHours(0, 0, 0, 0); // 设置为当天的0点

  // 获取昨天和明天的日期
  const yesterday = new Date(today);
  yesterday.setDate(today.getDate() - 1); // 昨天

  const tomorrow = new Date(today);
  tomorrow.setDate(today.getDate() + 1); // 明天

  // 比较日期并返回结果
  if (inputDate.getTime() === today.getTime()) {
    return "今天";
  } else if (inputDate.getTime() === yesterday.getTime()) {
    return "昨天";
  } else if (inputDate.getTime() === tomorrow.getTime()) {
    return "明天";
  } else {
    return "";
  }
}
//判断风向
function judgeWindDirectionByDegree(degree) {
  // 确保度数在0到360之间
  if (degree < 0 || degree >= 360) {
    return "输入的度数不合法，应该在0到360之间";
  }

  // 判断风向的区间范围
  if (degree >= 337.5 || degree < 22.5) {
    return "北风";
  } else if (degree >= 22.5 && degree < 67.5) {
    return "东北风";
  } else if (degree >= 67.5 && degree < 112.5) {
    return "东风";
  } else if (degree >= 112.5 && degree < 157.5) {
    return "东南风";
  } else if (degree >= 157.5 && degree < 202.5) {
    return "南风";
  } else if (degree >= 202.5 && degree < 247.5) {
    return "西南风";
  } else if (degree >= 247.5 && degree < 292.5) {
    return "西风";
  } else if (degree >= 292.5 && degree < 337.5) {
    return "西北风";
  }
}
//判断风速为几级
function judgeWindSpeed(speed) {
  // 确保输入的风速是非负数
  if (speed < 0) {
    return "风速不能为负数";
  }

  // 根据风速判断风级
  if (speed < 1) {
    return "0级";
  } else if (speed <= 5) {
    return "1级";
  } else if (speed <= 11) {
    return "2级";
  } else if (speed <= 19) {
    return "3级";
  } else if (speed <= 28) {
    return "4级";
  } else if (speed <= 38) {
    return "5级";
  } else if (speed <= 49) {
    return "6级";
  } else if (speed <= 61) {
    return "7级";
  } else if (speed <= 74) {
    return "8级";
  } else if (speed <= 88) {
    return "9级";
  } else if (speed <= 102) {
    return "10级";
  } else if (speed <= 117) {
    return "11级";
  } else if (speed <= 133) {
    return "12级";
  } else if (speed <= 149) {
    return "13级";
  } else if (speed <= 166) {
    return "14级";
  } else if (speed <= 183) {
    return "15级";
  } else if (speed <= 201) {
    return "16级";
  } else if (speed <= 220) {
    return "17级";
  } else {
    return "18级"
  }
}
//获取定位信息
function geocodeRegeo(location) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `https://restapi.amap.com/v3/geocode/regeo`,
      data: {
        location: location,
        key: '5d163405129542a96bdb5f6985c2cede',
      },
      method: 'GET'
    }).then((res) => {
      if (res.data.status == 1) {
        const cityRegeo = res.data.regeocode
        uni.setStorageSync("cityRegeo", res.data.regeocode.addressComponent)
        resolve(res.data.regeocode)
      }
    }).catch((err) => {
      reject(err)
      console.log(err, 999)
    })
  })
}
//通过用户ip获取位置
function geocodeIpRegeo(data) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `https://restapi.amap.com/v3/ip`,
      data: {
        ip: data,
        key: '5d163405129542a96bdb5f6985c2cede',
      },
      method: 'GET'
    }).then((res) => {
      if (res.data.status == 1) {
        uni.setStorageSync("cityRegeo", res.data)
        getLocation(res.data)
        resolve(res.data)
      }
    }).catch((err) => {
      reject(err)
      console.log(err, 999)
    })
  })
}
//将位置信息保存到本地
function getLocation(newItem) {
  let myArray = uni.getStorageSync('cityCache') || []
  let isExist = myArray.some(item => item.adcode === newItem.adcode);
  if (!isExist) {
    myArray.push(newItem)
    uni.setStorageSync('cityCache', myArray)
  } else {
    console.log('该元素已存在');
  }

}

export default {
  weekdate,
  getWeatherImage,
  getWeatherValue,
  getAirQuality,
  getAirQualitySimple,
  checkDateRelativeToToday,
  judgeWindDirectionByDegree,
  judgeWindSpeed,
  geocodeRegeo,
  getLocation,
  getWeatherBackground,
  getWeatherHomeImage,
  getWeatherHeaderBackground,
  geocodeIpRegeo
}